package com.kaigejava.ack;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by kaigejava on 2019/7/21.
 * 消费者限流的生产者
 */
public class Procuder4Ack {
    public static void main(String[] args) throws  Exception {
        //1:创建工厂对象
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setPort(5672);
        connectionFactory.setHost("192.168.31.130");
        connectionFactory.setVirtualHost("/");
        //2：通过工厂对象获取到连接对象
        Connection connection = connectionFactory.newConnection();
        //3:从连接对象中获取到消息通过channel对象
        Channel channel = connection.createChannel();
        String exchangeName = "test_ack_exchange";
        String routingKey = "ack.save";
        // 4:添加returnListener
      /*  channel.addReturnListener(new ReturnListener() {
            *//**
             *
             * @param replyCode     响应码
             * @param replyText     响应文本
             * @param exchange      交换机
             * @param routingKey    routingkey
             * @param properties    附加参数
             * @param body          消息体
             * @throws IOException
             *//*
            @Override
            public void handleReturn(int replyCode, String replyText, String exchange, String routingKey
                    , AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.err.println("---------handle  return----------");
                System.err.println("replyCode: " + replyCode);
                System.err.println("replyText: " + replyText);
                System.err.println("exchange: " + exchange);
                System.err.println("routingKey: " + routingKey);
                System.err.println("properties: " + properties);
                System.err.println("body: " + new String(body));
            }
        });*/
        //5:发送消息
        String message = "this message from RentrunListener";
        for(int i = 0;i<5;i++){
            Map<String,Object> handers = new HashMap<>();
            handers.put("num",i);
            AMQP.BasicProperties properties = new AMQP.BasicProperties().builder()
                    .deliveryMode(2)
                    .contentEncoding("UTF-8")
                    .headers(handers)
                    .build();
            channel.basicPublish(exchangeName,routingKey,true,properties,(message+i).getBytes());
        }
        System.out.println("===>>消息发送完成===>");
    }
}
